/*
 * @Author: your name
 * @Date: 2021-12-22 18:48:43
 * @LastEditTime: 2021-12-22 19:37:06
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \briup_am\am-server\app\service\team.js
 */
const { Service } = require('egg')
const BussinessError = require('../utils/BussinessError')



class TeamService extends Service {
    // 分页查询用户信息
    async pageQuery ({ page, pageSize }) {
        const { mysql } = this.app;
        
        //1. 查询分页数据
        let sql_select = `select * from match_team limit ${(page - 1) * pageSize},${pageSize}`
        const teams = await mysql.query(sql_select)
    
        //2. 统计数量
        const sql = `select count(*) as total from match_team`
        // 获取分页查询的数据条数
        const [{ total }] = await mysql.query(sql)
        // 返回
        return {
          page,
          pageSize,
          total,
          list: teams
        }
      }
      // 添加或修改用户信息
    async saveOrUpdate (row) {
        const { mysql } = this.app
        try {
          if (row.id) {
            // 更新 {id:1,name:"terry",age:21}  update match_player set name='terry',no=21 where id = 1
            await mysql.update('match_team', row)
          } else {
            await mysql.insert('match_team', row)
          }
        } catch (error) {
          console.log(error);
          throw new BussinessError('操作失败')
        }
      }
      
  // 根据id查询设备详情
  async findDeviceDetail (id) {
    const { mysql } = this.app
    try {
      const sql1 = `select * from match_player where id = ${id}`
      const [team] = await mysql.query(sql1)

      let detail = {}
      if (team.engineer_id) {
        const sql2 = `select * from match_team where id = ${team.engineer_id}`
        const [engineer] = await mysql.query(sql2)
        detail = {
          ...team,
          engineer
        }
      } else {
        detail = {
          ...team,
          engineer: null
        }
      }

      return detail
    } catch (error) {
      throw error
    }
  }
}
module.exports = TeamService
